<?php

namespace addons\tpmecms;

use app\common\library\Menu;
use think\Addons;


use think\Route;
use think\Db;
use think\Config;
/**
 * 插件
 */
class Tpmecms extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        
        $menu = [
            [
                'name'    => 'tpmecms',
                'title'   => 'TpMeCMS',
                'icon'    => 'fa fa-window-maximize',
                'sublist' => [
                    
                    [
                        'name'    => 'tpmecms/cate',
                        'title'   => '栏目管理',
                        'icon'    => 'fa fa-list-ol',
                        'remark'  => '栏目管理列表',
                        'sublist' => [
                            ['name' => 'tpmecms/cate/index', 'title' => '查看'],
                            ['name' => 'tpmecms/cate/add', 'title' => '添加栏目'],
                            ['name' => 'tpmecms/cate/del', 'title' => '删除栏目'],                            
                        ]
                    ],
                    [
                        'name'    => 'tpmecms/news',
                        'title'   => '新闻动态',
                        'icon'    => 'fa fa-newspaper-o',
                        'remark'  => '新闻动态',
                        'sublist' => [
                            ['name' => 'tpmecms/news/index', 'title' => '查看'],
                            ['name' => 'tpmecms/news/del', 'title' => '删除新闻'],
                            ['name' => 'tpmecms/news/add', 'title' => '添加新闻'],
                        ]
                    ],
                    [
                        'name'    => 'tpmecms/page',
                        'title'   => '单页',
                        'icon'    => 'fa fa-pagelines',
                        'remark'  => 'CMS单页管理',
                        'sublist' => [
                            ['name' => 'tpmecms/page/index', 'title' => '查看'],
                            ['name' => 'tpmecms/page/del', 'title' => '删除单页'],
                            ['name' => 'tpmecms/page/add', 'title' => '添加单页'],
                        ]
                    ],
              
                ]
            ]
        ];
        Menu::create($menu);
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        Menu::delete('tpmecms');
        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {
        Menu::enable('tpmecms');
        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {
        Menu::disable('tpmecms');
        return true;
    }

    /**
     * 实现钩子方法
     * @return mixed
     */
    public function testhook($param)
    {
        // 调用钩子时候的参数信息
        print_r($param);
        // 当前插件的配置信息，配置信息存在当前目录的config.php文件中，见下方
        print_r($this->getConfig());
        // 可以返回模板，模板文件默认读取的为插件目录中的文件。模板名不能为空！
        //return $this->fetch('view/info');
    }


     /**
     * 应用初始化
     */
    public function appInit()
    {
        $config = get_addon_config('tpmecms');
        if($config['route_switch']=='1')
        foreach(Db::name('tpmecms_cate')->select() as $key=>$val){

            switch ($val['type']) {
                case 'page':

                    Route::get($val['diyname'],'/addons/tpmecms/index/cate?id='.$val['id']);
                    break;
                case 'list':
                    Route::get($val['diyname'],'/addons/tpmecms/index/cate?id='.$val['id']);

                    Route::get($val['diyname'].'/page/:id','/addons/tpmecms/index/cate/id/'.$val['id']);

                    Route::get($val['diyname'].'_show/:id','/addons/tpmecms/index/show?cate_id='.$val['id']);
                    break;
                default:
                    # code...
                    break;
            }
        }
    }

}
